#!usr/bin/env python
#-*- coding:utf-8 -*-
"""
数据源：巨潮 -> 市场资讯 -> 上市公司 -> 公司资讯 -> 最新资料 ->右栏：公司备忘
爬取类目：公司备忘
股票类型不包括港股（港股没有最新资料）
url_sample: http://www.cninfo.com.cn/information/memo/szmb000001.html
"""
"""
result_sample_of_company_memo = {
    '_id': '',
    'stockCode': '',
    'companyShortName': '',
    'stockType': '',
    'memo':[{
        'event',
        'date',
    },],
}
"""

import sys
import json
import datetime
from xtls.logger import get_logger

from company_formaltable_crawler import CompanyFormalTable

reload(sys)
sys.setdefaultencoding('utf-8')

CATEGORY = 'memo'
def get_detail_mapping():
    DETAIL_MAPPING = {
        u'备忘事件': 'event',
        u'备忘时间': 'date',
    }
    return DETAIL_MAPPING

class CompanyMemo(CompanyFormalTable):
    def __init__(self, stock_list):
        logger = get_logger(__file__)
        super(CompanyMemo, self).__init__(stock_list=stock_list, logger=logger)
        self.select_path = 'div.zx_list'
        self.category = 'memo'
        self.tab_name = 'memo'
        self.detail_mapping = get_detail_mapping()

    def save_update(self, data):
        if not data.get('memo', None):
            self.logger.warn('memo has no information %s-%s' % (data.get('stock_id'), data.get('stock_code')))
            return
        if not data.get('stock_id'):
            return

        version_num = 1 + self.select_current_version_num(table_name='memo', stock_id=data.get('stock_id'))
        for single in data.get('memo', dict()):
            self.cleaner.set_dirty_data(single)
            single_clean = self.cleaner.complete()

            single_clean['stockId'] = data.get('stock_id', None)
            single_clean['versionNum'] = version_num
            self.insert_to_db(table_name='memo', cleaned_data=single_clean)

    def save_insert(self, data):
        if not data.get('memo', None):
            self.logger.warn('memo has no information %s-%s' % (data.get('stock_id'), data.get('stock_code')))
            return
        if not data.get('stock_id'):
            return

        lastest_date = self.select_lastest_date(table_name='memo',
                                                field_name='date',
                                                stock_id=data.get('stock_id'))
        version_num = self.select_current_version_num(table_name='memo', stock_id=data.get('stock_id'))
        version_num = 1 if version_num == 0 else version_num

        for single in data.get('memo', dict()):
            self.cleaner.set_dirty_data(single)
            single_clean = self.cleaner.complete()
            cur_date = single_clean.get('date')
            if not self.check_lastest(cur_date, lastest_date):
                continue

            single_clean['stockId'] = data.get('stock_id', None)
            single_clean['versionNum'] = version_num

            self.insert_to_db(table_name='memo', cleaned_data=single_clean)

    def save_print(self, data):
        print json.dumps(data)
        print "=" * 70


def main():
    pass

if __name__ == '__main__':
    main()
